package com.aidex.biz.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAdjusters;

/**
 * 日期间隔工具类
 * 提供常用的时间范围计算
 */
public class DateRangeUtil {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
    private static final DateTimeFormatter YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy");


    /**
     * 构建查询参数列表（开始时间 + 结束时间）
     */
    public static Object[] buildParams(String startDate, String endDate) {
        return new Object[]{startDate, endDate};
    }

    // ==================== 今日 ====================

    public static String getTodayStart() {
        return LocalDate.now().format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getTodayEnd() {
        return LocalDate.now().format(DATE_FORMATTER) + " 23:59:59";
    }

    public static String getTodayDate() {
        return LocalDate.now().format(DATE_FORMATTER);
    }


    // ==================== 昨日 ====================

    public static String getYesterdayStart() {
        return LocalDate.now().minusDays(1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getYesterdayEnd() {
        return LocalDate.now().minusDays(1).format(DATE_FORMATTER) + " 23:59:59";
    }

    public static String getYesterdayDate() {
        return LocalDate.now().minusDays(1).format(DATE_FORMATTER);
    }


    // ==================== 本周 ====================

    public static String getWeekStart() {
        return LocalDate.now().with(java.time.DayOfWeek.MONDAY).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getWeekEnd() {
        return LocalDate.now().with(java.time.DayOfWeek.SUNDAY).format(DATE_FORMATTER) + " 23:59:59";
    }


    // ==================== 本月 ====================

    public static String getMonthStart() {
        return LocalDate.now().withDayOfMonth(1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getMonthEnd() {
        return LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).format(DATE_FORMATTER) + " 23:59:59";
    }

    public static String getMonth() {
        return LocalDate.now().format(MONTH_FORMATTER);
    }


    // ==================== 上月 ====================

    public static String getLastMonthStart() {
        return LocalDate.now().minusMonths(1).withDayOfMonth(1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getLastMonthEnd() {
        return LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).format(DATE_FORMATTER) + " " +
                "23:59:59";
    }

    public static String getLastMonth() {
        return LocalDate.now().minusMonths(1).format(MONTH_FORMATTER);
    }


    // ==================== 本季度 ====================

    public static String getQuarterStart() {
        LocalDate now = LocalDate.now();
        int quarter = (now.getMonthValue() - 1) / 3;
        int month = quarter * 3 + 1;
        return LocalDate.of(now.getYear(), month, 1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getQuarterEnd() {
        LocalDate now = LocalDate.now();
        int quarter = (now.getMonthValue() - 1) / 3;
        int month = (quarter + 1) * 3;
        LocalDate lastDay = LocalDate.of(now.getYear(), month, 1).with(TemporalAdjusters.lastDayOfMonth());
        return lastDay.format(DATE_FORMATTER) + " 23:59:59";
    }

    public static int getQuarter() {
        return LocalDate.now().get(IsoFields.QUARTER_OF_YEAR);
    }


    // ==================== 上季度 ====================

    public static String getLastQuarterStart() {
        LocalDate now = LocalDate.now().minusMonths(3);
        int quarter = (now.getMonthValue() - 1) / 3;
        int month = quarter * 3 + 1;
        return LocalDate.of(now.getYear(), month, 1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getLastQuarterEnd() {
        LocalDate now = LocalDate.now().minusMonths(3);
        int quarter = (now.getMonthValue() - 1) / 3;
        int month = (quarter + 1) * 3;
        LocalDate lastDay = LocalDate.of(now.getYear(), month, 1).with(TemporalAdjusters.lastDayOfMonth());
        return lastDay.format(DATE_FORMATTER) + " 23:59:59";
    }

    public static int getLastQuarter() {
        return LocalDate.now().minusMonths(3).get(IsoFields.QUARTER_OF_YEAR);
    }


    // ==================== 今年 ====================

    public static String getYearStart() {
        return LocalDate.now().withDayOfYear(1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getYearEnd() {
        return LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).format(DATE_FORMATTER) + " 23:59:59";
    }

    public static String getYear() {
        return LocalDate.now().format(YEAR_FORMATTER);
    }


    // ==================== 去年 ====================

    public static String getLastYearStart() {
        return LocalDate.now().minusYears(1).withDayOfYear(1).format(DATE_FORMATTER) + " 00:00:00";
    }

    public static String getLastYearEnd() {
        return LocalDate.now().minusYears(1).with(TemporalAdjusters.lastDayOfYear()).format(DATE_FORMATTER) + " 23:59" +
                ":59";
    }

    public static String getLastYear() {
        return LocalDate.now().minusYears(1).format(YEAR_FORMATTER);
    }


    // ==================== 自定义日期范围 ====================

    public static String formatDateTime(LocalDateTime dateTime) {
        return dateTime.format(DATE_TIME_FORMATTER);
    }

    public static String formatDate(LocalDate date) {
        return date.format(DATE_FORMATTER);
    }

    public static String formatDate(LocalDate date, String pattern) {
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }
}